﻿using RightControl.Common;
using RightControl.IRepository;
using RightControl.IService;
using RightControl.Model;
using RightControl.Repository;
using System;
using System.Collections.Generic;
using System.Text;

namespace RightControl.Service {
    public class AmazonService : BaseService<AmazonMeta>, IAmazonService {
        public IAmazonRepository repository { get; set; }
        public static IBaseCategoryService baseCategoryService { get; set; }
        public static IExportFieldsService exportFieldsService { get; set; }

        public dynamic GetListByFilter(AmazonMeta filter, PageInfo pageInfo) {
            string _where = " where 1=1";
            if (!string.IsNullOrEmpty(filter.CategoryCode)) {
                _where += string.Format(" and CategoryCode = '{0}'", filter.CategoryCode);
            }
            _where += string.Format(" and CreateBy = {0}", filter.CreateBy);

            return GetListByFilter(filter, pageInfo, _where);
        }

        public int AddAmazonMeta(ProductDetail productDetail,int userId) {
            if (productDetail != null) {
                string categoryName = baseCategoryService.GetCategoryName(productDetail.CategoryCode);
                var gradeFields = exportFieldsService.GetGradeFields(productDetail.CategoryCode);
                Dictionary<String, String> attrDic = SerializerHelper.JsonDeserialized<Dictionary<string, string>>(productDetail.KeyAttributes);

                AmazonMeta metaData = new AmazonMeta();
                metaData.ProductId = productDetail.Id;
                metaData.CategoryCode = productDetail.CategoryCode;
                metaData.ProductSku = string.Format("{0}{1}", productDetail.SkuId, Common.Common.RandomChar());
                metaData.ImageUrl = productDetail.ImageUrl;
                metaData.Title = productDetail.Title;

                StringBuilder sbDesc = new StringBuilder();
                Dictionary<String, String> keyAttr = SerializerHelper.JsonDeserialized<Dictionary<String, String>>(productDetail.KeyAttributes);
                foreach (KeyValuePair<String, String> item in keyAttr) {
                    if (!string.IsNullOrEmpty(item.Value)) {
                        sbDesc.AppendFormat("{0}:{1}</br>", item.Key.Replace("_", " "), item.Value);
                    }
                }
                metaData.Description = sbDesc.ToString();

                string gFeature = gradeFields.ContainsKey("G") ? gradeFields["G"] : gradeFields["H"];
                metaData.BulletPoints1 = "Please check the size chart (Last Image) before purchasing | Please Note: Color may vary slightly from image;";
                //Size: (inch) Dresses Length: 32.4; Bust: 32.8; Waist: 66; Hipline: 32.8; Shoulder: 0; Sleeve length: 0.
                //                metaData.BulletPoints2 = String.Format("Size: (inch) Bust:{0} Waist:{1} Hip:{2} Shoulder：{3}", "");//英寸单位  1厘米(cm)=0.3937008英寸(in)
                //Style Type, +Dresses Shape, +Dresses Length, +Waist Type, +Closure Type
                metaData.BulletPoints2 = attrDic[gradeFields["I"]];
                metaData.BulletPoints3 = attrDic[gFeature];
                metaData.BulletPoints4 = string.Format("This {0} is perfect for vacation, outdoor, wedding, cocktail, party, work or casual occation.", categoryName);
                //metaData.BulletPoints3 = string.Format("{0} {1} {2} {3} {4}", attrDic["Style_Type"], attrDic["Dresses_Shape"], attrDic["Dresses_Length"], attrDic["Waistline"], attrDic["Closure_Type"]);
                metaData.BulletPoints5 = String.Format("Washing introduction: hand wash and hang to dry.");
            
                //子sku
                //Item Type等级值,Gender等级值,Gender-Item Type等级值,Color-Item Type等级值,Dress Length-Item Type等级值
                metaData.KeyWord1 = string.Format("{0} {1}", categoryName, attrDic[gradeFields["B"]]);
                metaData.KeyWord2 = string.Format("{0} {1}", categoryName, attrDic[gradeFields["C"]]);
                metaData.KeyWord3 = categoryName;
                metaData.KeyWord4 = string.Format("{0} {1}", categoryName, attrDic[gradeFields["D"]]);
                metaData.KeyWord5 = string.Format("{0} {1} {2}", attrDic[gradeFields["B"]], attrDic[gradeFields["C"]], categoryName);

                metaData.CreateOn = DateTime.Now;
                metaData.CreateBy = userId;
                return this.InsertModel(metaData);
            }
            else {
                return -1;
            }

        }

    }
}